package com.turing.data.scripts.yhlc.filter


import com.turing.common.util.FileUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils

import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * @description: 银行理财 - 产品名称
 * @author: mazhishen
 */

class ProductName_30247_46_filter {

    public static void main(String[] args) {
        /*DependParamsVO param = new DependParamsVO()
        param.setJSONPathMap(new LinkedHashMap<String, String>())
        param.getJSONPathMap().put("abc", "http://media.tlrobot.com/group1/M00/0D/31/rBIHD2DdbtWAJVfLAAApilMZenU13.json")
        List<TaskElementVO.DrawResultVO> s = doConvert(null, param)*/
        println "温州银行金鹿理财-温盈19005号开放式净值型银行理财计划".length()+2
    }

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_BACKUP_1 = "投资者名称"
        def REGEX_BACKUP_2 = "客户名称"
        def REGEX_BACKUP_3 = "尊敬的"
        def REGEX_BACKUP_4 = "产品名称"

        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                //往前10个字符搜索
                while( expandOffset < 20 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if( expandString =~ REGEX_BACKUP_1
                        || expandString =~ REGEX_BACKUP_2
                        || expandString =~ REGEX_BACKUP_3
                        || expandString =~ REGEX_BACKUP_4
                    )
                    {
	try{
                        transfer(rawText, dependParams, childTaskSn, drawResultVO, trueValueSet)}
catch(Exception e){
            e.printStackTrace()
        }
                        if(trueValueSet.size()>0){
                            return trueValueSet
                        }
                        drawResultVO.businessValue = drawResultVO.text.replaceAll(/(尊敬的)/,"")
                        if(drawResultVO.businessValue.contains("（")&&!drawResultVO.businessValue.contains("）")){
                            drawResultVO.businessValue = drawResultVO.businessValue.concat("）")
                        }
                        drawResultVO.type = 1
                        trueValueSet.add(drawResultVO)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        try{
            if(trueValueSet.size()==0){
                def REGEX_START_1 = /产品名称.{1,40}计划|尊敬的.{1,30}计划\)/

                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX_START_1,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )
                if(trueValueSet.size()>0){
                    def textPathMap = dependParams.textPathMap
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)
                    def text = trueValueSet[0].text
                    def regexUsed = ""
                    def index = text.indexOf("产品名称")
                    if(index == -1){
                        index = text.indexOf("尊敬的")
                        regexUsed = "尊敬的"
                    }else{
                        regexUsed = "产品名称"
                    }
                    def startOffset = index + regexUsed.length()
                    def endOffset = 0
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    transfer(rawText, dependParams, childTaskSn, objectNew, trueValueSet)
                    if(trueValueSet.size()>0){
                        return trueValueSet
                    }
                    return trueValueSet
                }

                //实在找不到 再去找投资者名称
                if(trueValueSet.size()==0) {
                    def REGEX_INVEST = /投资者名称.{1,60}第\d期/

                    dependParams.JSONTextMap.forEach(
                            { childTaskSn, jsonText ->
                                RegexExtract.doRuleExtract(REGEX_INVEST, childTaskSn, dependParams, jsonText, trueValueSet)
                            }
                    )
                    if (trueValueSet.size() > 0) {
                        def textPathMap = dependParams.textPathMap
                        def childTaskSn = trueValueSet[0].childTaskSn
                        def rawPath = textPathMap.get(childTaskSn)
                        def rawText = FileUtils.loadJson(rawPath)
                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(childTaskSn)
                        def text = trueValueSet[0].text
                        def index = text.indexOf("投资者名称")
                        def regexUsed = "投资者名称"
                        def startOffset = index + regexUsed.length()
                        def endOffset = 0
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                        trueValueSet.clear()
                        trueValueSet.add(objectNew)
                        transfer(rawText, dependParams, childTaskSn, objectNew, trueValueSet)
                        if(trueValueSet.size()>0){
                            return trueValueSet
                        }
                        return trueValueSet
                    }
                }

                //实在找不到 再去找投资者名称
                if(trueValueSet.size()==0) {
                    def REGEX_INVEST = /账户名称:.{1,60}(第.{1,4}。|年。)/

                    dependParams.JSONTextMap.forEach(
                            { childTaskSn, jsonText ->
                                RegexExtract.doRuleExtract(REGEX_INVEST, childTaskSn, dependParams, jsonText, trueValueSet)
                            }
                    )
                    if (trueValueSet.size() > 0) {
                        def textPathMap = dependParams.textPathMap
                        def childTaskSn = trueValueSet[0].childTaskSn
                        def rawPath = textPathMap.get(childTaskSn)
                        def rawText = FileUtils.loadJson(rawPath)
                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(childTaskSn)
                        def text = trueValueSet[0].text
                        def index = text.indexOf("账户名称:")
                        def regexUsed = "账户名称:"
                        def startOffset = index + regexUsed.length()
                        def endOffset = 0
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                        trueValueSet.clear()
                        trueValueSet.add(objectNew)
                        transfer(rawText, dependParams, childTaskSn, objectNew, trueValueSet)
                        if(trueValueSet.size()>0){
                            return trueValueSet
                        }
                        return trueValueSet
                    }
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = ""
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

    }

    private static void transfer(String rawText, DependParamsVO dependParams, String childTaskSn, TaskElementVO.DrawResultVO drawResultVO, ArrayList<TaskElementVO.DrawResultVO> trueValueSet) {
        if (rawText.contains("桂林银行股份有限公司")) {
            def fileNames = dependParams.fileNames
            def fileName = fileNames.get(childTaskSn)
            def text = drawResultVO.text
            text = text.replaceAll("\n", "")
            def JSONTextMap = dependParams.JSONTextMap
            def fileTextStr = ""
            fileTextStr = JSONTextMap.get(childTaskSn)
            if (fileName.contains("惠享") || fileName.contains("汇尊")) {
                if (text.contains("-")) {
                    def startOffset = text.indexOf("-") + ("-").length()
                    //对解决这种情况：桂林银行股份有限公司一漓江理财桂享2021-65期
                    if(text.contains("一")){
                        startOffset = text.indexOf("一")+("一").length()
                    }
                    def endOffset = 0
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    //return trueValueSet
                }
            } else if (fileName.contains("迎利")) {
                if (text.contains("桂林银行股份有限公司")) {
                    def startOffset = text.indexOf("桂林银行股份有限公司")
                    def endOffset = -(text.length() - (text.indexOf("桂林银行股份有限公司") + "桂林银行股份有限公司".length()))
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    //return trueValueSet
                }
            }
        }
    }


}
